﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

/// <summary>
/// Summary description for ManagementUser
/// </summary>
public class ManagementUser
{
    SqlConnection coon;
	public ManagementUser()
	{
        coon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
	}
    /*********** User ***********/

    public DataSet GetUser()
    {
        if (coon.State == ConnectionState.Closed)
        {
            coon.Open();
        }
        DataSet dt = new DataSet();
        string sql = "select * from Manager order by ManagerID DESC";
        SqlDataAdapter sda = new SqlDataAdapter(sql, coon);
        sda.Fill(dt);
        return dt;
    }

    public bool DeleteUser(string id)
    {
        if (coon.State == ConnectionState.Closed)
            coon.Open();
        string sql = "DELETE FROM  Manager where ManagerID = '" + id + "'";
        SqlCommand cmd = new SqlCommand(sql, coon);
        int i = cmd.ExecuteNonQuery();

        coon.Close();
        return i > 0 ? true : false;
    }

    public DataSet SearchUser(string s)
    {
        if (coon.State == ConnectionState.Closed)
            coon.Open();
        string sql = "select * from Manager where Name like '%" + s + "%' order by ManagerID DESC";
        DataSet dt = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter(sql, coon);
        sda.Fill(dt);

        return dt;
    }


    public bool UpdateUser(string id, string name, string phone, string email, string password)
    {
        if (coon.State == ConnectionState.Closed)
        { coon.Open(); }

        string sql = "update Manager set Name='" + name + "',Phone='" + phone + "',Email='" + email + "',[Password] = '" + password + "'  where ManagerID='" + id + "' ";
        SqlCommand cmd = new SqlCommand(sql, coon);


        int i = cmd.ExecuteNonQuery();
        coon.Close();
        return i > 0 ? true : false;
    }


    public bool AddUser(string ID, string name, string phone, string email, string pass)
    {

        if (coon.State == ConnectionState.Closed)
            coon.Open();
        string sql = "insert into Manager values(@id,@n,@p,@e,@pass)";
        SqlCommand cmd = new SqlCommand(sql, coon);
        cmd.Parameters.AddWithValue("@id", ID);
        cmd.Parameters.AddWithValue("@n", name);
        cmd.Parameters.AddWithValue("@p", phone);
        cmd.Parameters.AddWithValue("@e", email);
        cmd.Parameters.AddWithValue("@pass", pass);
        int i = cmd.ExecuteNonQuery();
        coon.Close();
        return i > 0 ? true : false;
    }
    public ORANGE_PROJECTModel.Manager GetEditUser(string id)
    {
        if (coon.State == ConnectionState.Closed)
            coon.Open();
        DataSet dt = new DataSet();
        string sql = "select * from Manager where ManagerID='" + id + " '";
        SqlCommand cmd = new SqlCommand(sql, coon);
        SqlDataReader sda = cmd.ExecuteReader();
        ORANGE_PROJECTModel.Manager m = new ORANGE_PROJECTModel.Manager();
        while (sda.Read())
        {
            m.Name = sda.GetString(1);
            m.Phone = sda.GetString(2);
            m.Email = sda.GetString(3);
            m.Password = sda.GetString(4);
        }
        coon.Close();
        return m;
    }

    public bool CheckUsername(string s)
    {
        if (coon.State == ConnectionState.Closed)
            coon.Open();
        string sql = "select * from Manager where ManagerID='" + s + "'";
        SqlCommand cmd = new SqlCommand(sql, coon);
        SqlDataReader sda = cmd.ExecuteReader();

        int i = 0;
        if (sda.HasRows)
        { i = 1; }
        sda.Close();
        return i == 1 ? true : false;
    }

    public ORANGE_PROJECTModel.Manager checkLogin(string u, string p)
    {
        if (coon.State == ConnectionState.Closed)
            coon.Open();
        string sql = "select * from Manager where ManagerID='" + u + "' and [Password]='" + p + "'";
        SqlCommand cmd = new SqlCommand(sql, coon);
        SqlDataReader sdr = cmd.ExecuteReader();
        ORANGE_PROJECTModel.Manager m = new ORANGE_PROJECTModel.Manager();
        if (sdr.HasRows)
        {
            while (sdr.Read())
            {
                m.ManagerID = sdr.GetString(0);
                m.Name = sdr.GetString(1);
            }
            return m;
        }
        else
        {
            return m;
        }
    }
}